function GetQueryString(param) {
	var request = {
		QueryString : function(val) {
			var uri = window.location.search;
			var re = new RegExp("" + val + "=([^&?]*)", "ig");
			return ((uri.match(re)) ? (decodeURI(uri.match(re)[0]
					.substr(val.length + 1))) : '');
		}
	}
	return request.QueryString(param);
}
var rtmp_publish_url = "";
var rtmp_play_url = "";
var hls_play_url = "";
var lssapp_id = GetQueryString("lssapp_id");
var stream_stu = GetQueryString("stream_stu");
var objectPlayer;

function closeme() {
	if (objectPlayer)
		objectPlayer.closeConnect();
}

function initParam(){
	$.ajax({
		type : "POST",
		timeout : 8000,
		url : "./remoteinvoke.php",
		dataType : "json",
		data : {
			method : 's_url'
		},
		success : function(data) {
			if (data.Flag == 100) {
				rtmp_publish_url = data.rtmp_publish_url;
				rtmp_play_url = data.rtmp_play_url;
				hls_play_url = data.hls_play_url;
				initPlay();
			} else {
				return;
			}
		},
		error : function(error) {
			return;
		}
	});
}

function genK(option) {
	$.ajax({
		type : "POST",
		timeout : 8000,
		url : "./remoteinvoke.php",
		dataType : "json",
		data : {
			method : 'genK',
			appid : option.appid,
			stream : option.stream
		},
		success : function(data) {
			if (data.Flag == 100) {
				if (option.success)
					option.success(data);
			} else {
				if (option.failure)
					option.failure(data.FlagString);
			}
		},
		error : option.failure
	});
}

function initPlay(){
	var stream_stu_tmp = stream_stu.replace(/-/g,'_');
	genK({
		appid : lssapp_id,
		stream : stream_stu_tmp,
		success : function(data) {
			console.log(data);
			objectPlayer = new aodianPlayer({
				container : 'stuPlay',
				rtmpUrl : rtmp_play_url + '/' + lssapp_id + '/' + stream_stu_tmp + '?k=' + data.k + '&t=' + data.t,
				hlsUrl : hls_play_url + '/' + lssapp_id + '/' + stream_stu_tmp + '.m3u8' + '?k=' + data.k + '&t=' + data.t,
				width : '278',
				height : '180',
				autostart : true,
				bufferlength : '1',
				maxbufferlength : '2',
				stretching : '1',
				controlbardisplay : 'disable',
				adveDeAddr:'images/yt_02.png',
		        adveWidth:278,
		        adveHeight:130,
		        adveReAddr:'http://www.ykyueke.com'
			});
			
		},
		failure : function(errStr) {
			alert(errStr.responseText);
		}
	});
	
}

$(function() {
	initParam();
});
